package com.bd.model;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.bd.constants.Action;
import com.bd.constants.BDConstants;
import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

public class TableCreator {

	private StringBuilder	sb;
	private String			tableName;

	public TableCreator(String tableName) {
		this.tableName = tableName;
		sb = new StringBuilder("CREATE TABLE ");
		sb.append(tableName);
		sb.append(" (\n");
	}

	public void addColumn(String name, String type) {
		sb.append("\t");
		sb.append(name);
		sb.append(" " + type + ",");
	}

	public void addColumn(String name, String type, String... parameters) {
		sb.append("\t");
		sb.append(name);
		sb.append(" " + type + " ");

		for (String s : parameters)
			sb.append(s + " ");
		sb.append(", ");
	}

	public void addColumn(String name, String type, int length) {
		sb.append("\t");
		sb.append(name);
		sb.append(" " + type);
		sb.append("(" + length + "),");
	}

	public void addColumn(String name, String type, int length, String... parameters) {
		sb.append("\t");
		sb.append(name);
		sb.append(" " + type);
		sb.append("(" + length + ") ");

		for (String s : parameters)
			sb.append(s + " ");
		sb.append(", ");
	}

	public void setPrimaryKey(String name) {
		sb.append("\t CONSTRAINT pk_");
		sb.append(tableName);
		sb.append("_" + name);
		sb.append(" PRIMARY KEY (");
		sb.append(name);
		sb.append("), ");
	}

	public void addForeignKey(String columnName, String referenceTable, String referenceColumn, Action onDeleteAction, Action onUpdateAction) {
		sb.append("\t CONSTRAINT fk_");
		sb.append(tableName);
		sb.append("_" + columnName);
		sb.append(" FOREIGN KEY (");
		sb.append(columnName);
		sb.append(") REFERENCES ");
		sb.append(referenceTable);
		sb.append("(" + referenceColumn + ") \n\t");
		sb.append(onDeleteAction);
		sb.append("\n\t" + onUpdateAction + ", ");
	}

	public void displaySQL() {
		final JFrame frame = new JFrame();
		JPanel contentPane = new JPanel();

		String columns = "3dlu, default:grow, 3dlu";
		String lines = "3dlu, default:grow, 3dlu, 25dlu, 3dlu";

		sb.replace(sb.lastIndexOf(","), sb.lastIndexOf(",") + 1, "\n)");

		contentPane.setLayout(new FormLayout(columns, lines));
		final JTextArea jA = new JTextArea(sb.toString().replaceAll(",", ",\n"));
		jA.setLineWrap(true);
		jA.setWrapStyleWord(true);
		JScrollPane pane = new JScrollPane(jA);
		JButton btnClose = new JButton("Copiar");
		btnClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Clipboard clp = Toolkit.getDefaultToolkit().getSystemClipboard();
				StringSelection sl = new StringSelection(jA.getText());
				clp.setContents(sl, null);
			}
		});

		contentPane.add(pane, CC.xy(2, 2, CC.FILL, CC.FILL));
		contentPane.add(btnClose, CC.xy(2, 4, CC.FILL, CC.FILL));
		frame.setContentPane(contentPane);

		frame.setSize(new Dimension(600, 200));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.pack();
	}

	public static void main(String[] args) {
		TableCreator tb = new TableCreator("TESTE");
		tb.addColumn("id", BDConstants.INTEGER, 11, BDConstants.AUTO_INCREMENT, BDConstants.NOT_NULL);
		tb.addColumn("fkCodigo", BDConstants.INTEGER, 11);
		tb.addColumn("texto", BDConstants.VARCHAR, 255);
		tb.addColumn("texto2", BDConstants.VARCHAR, 255);
		tb.addColumn("texto3", BDConstants.VARCHAR, 255);
		tb.addColumn("texto4", BDConstants.VARCHAR, 255, BDConstants.NOT_NULL, BDConstants.UNIQUE);
		tb.setPrimaryKey("id");
		tb.addForeignKey("fkcodigo", "CD", "id", Action.ON_DELETE_SET_NULL, Action.ON_UPDATE_NO_ACTION);
		tb.displaySQL();

	}
}
